unit SQLiteConfig;

interface

uses
  Classes, SysUtils, IniFiles;

const
  SECTION_INI = 'Config';
  SECTION_LIB = 'Library';

type
  TSQLiteConfig = class
  private
    FLanguage:        integer;
    FEncodeFmt:       integer;
    FBatchSQL:        boolean;
    FDataReadonly:    boolean;
    FWordWrap:        boolean;
    FNoModify:        boolean;
    FUseSqliteSyntax: boolean;
    FFontSize:        integer;
    FFontName:        string;
    FViewStatusbar:   boolean;
    FViewObjTree:     boolean;
    FViewToolbar:     boolean;
    FHKKey:           integer;
    FHKDataType:      integer;
    FHKFunc:          integer;
    FUTF8FileName: Boolean;
    FCurrentLib: string;
    FLibList: TStringList;
    function GetLibrarys(Index: integer): string;
    procedure SetLibrarys(Index: integer; const Value: string);
    function GetLibraryCount: Integer;
  public
    constructor Create;
    destructor Destroy; override;
    procedure LoadFromFile(AFileName: string);
    procedure SaveToFile(AFileName: string);
    procedure AddLib(AFile: String);
    procedure DelLib(AFile: string);
  public

    property Librarys[Index: integer]:string Read GetLibrarys write SetLibrarys;
    property libraryCount: Integer read GetLibraryCount;
    property Language: integer Read FLanguage Write FLanguage;
    property BatchSQL: boolean Read FBatchSQL Write FBatchSQL;
    property WordWrap: boolean Read FWordWrap Write FWordWrap;
    property NoModify: boolean Read FNoModify Write FNoModify;
    property DataReadonly: boolean Read FDataReadonly Write FDataReadonly;
    property EncodeFmt: integer Read FEncodeFmt Write FEncodeFmt;
    property FontName: string Read FFontName Write FFontName;
    property FontSize: integer Read FFontSize Write FFontSize;
    property ViewObjTree: boolean Read FViewObjTree Write FViewObjTree;
    property ViewToolbar: boolean Read FViewToolbar Write FViewToolbar;
    property ViewStatusbar: boolean Read FViewStatusbar Write FViewStatusbar;

    property HKKey: integer Read FHKKey Write FHKKey;
    property HKFunc: integer Read FHKFunc Write FHKFunc;
    property HKDataType: integer Read FHKDataType Write FHKDataType;

    property UTF8FileName: Boolean read FUTF8FileName write FUTF8FileName;
    property CurrentLib: string read FCurrentLib write FCurrentLib;
    // inner auto config
    property UseSqliteSyntax: boolean Read FUseSqliteSyntax Write FUseSqliteSyntax;
  end;

implementation

{ TSQLiteConfig }

procedure TSQLiteConfig.AddLib(AFile: String);
begin
  if FLibList.IndexOf(AFile) = -1 then
    FLibList.Add(AFile);
end;

constructor TSQLiteConfig.Create;
begin
  FLibList:= TStringList.Create;
end;

procedure TSQLiteConfig.DelLib(AFile: string);
begin
  if FLibList.IndexOf(AFile) > -1 then
    FLibList.Delete(FLibList.IndexOf(AFile));
end;

destructor TSQLiteConfig.Destroy;
begin
  FLibList.Free;
  inherited;
end;

function TSQLiteConfig.GetLibraryCount: Integer;
begin
  Result := FLibList.Count;
end;

function TSQLiteConfig.GetLibrarys(Index: integer): string;
begin
  Result := FLibList[Index];
end;

procedure TSQLiteConfig.LoadFromFile(AFileName: string);
var
  i: integer;
  tmpLst: TStringList;
  s: string;
begin
  with TIniFile.Create(AFileName) do
  begin
    FLanguage     := ReadInteger(SECTION_INI, 'Lanuage', 1);
    FEncodeFmt    := ReadInteger(SECTION_INI, 'Encoding', 0);
    FBatchSQL     := ReadBool(SECTION_INI, 'BatchSQL', False);
    FDataReadonly := ReadBool(SECTION_INI, 'ReadOnly', False);
    FWordWrap     := ReadBool(SECTION_INI, 'Wordwrap', True);
    FNoModify     := ReadBool(SECTION_INI, 'AllowModify', True);

    FFontSize      := ReadInteger(SECTION_INI, 'FontSize', 8);
    FFontName      := ReadString(SECTION_INI, 'FontName', 'Tahoma');
    FViewStatusbar := ReadBool(SECTION_INI, 'ViewStatusBar', True);
    FViewObjTree   := ReadBool(SECTION_INI, 'ViewObjTree', True);
    FViewToolbar   := ReadBool(SECTION_INI, 'ViewToolBar', True);

    FHKKey      := ReadInteger(SECTION_INI, 'HKKey', 32880);   // Alt+F1
    FHKDataType := ReadInteger(SECTION_INI, 'HKType', 32882);  // Alt+F3
    FHKFunc     := ReadInteger(SECTION_INI, 'HKFunc', 32881);  // Alt+F2

    FUTF8FileName := ReadBool(SECTION_INI, 'UTF8FileName', False);
    FCurrentLib := ReadString(SECTION_INI, 'CurrentLib', 'SQLite3.dll');
    //  read library list
    tmpLst:= TStringList.Create;
    FLibList.Clear;
    ReadSectionValues(SECTION_LIB, tmpLst);
    for i := 0 to tmpLst.Count - 1 do
    begin
      s := tmpLst[i];
      System.Delete(s, 1, Pos('=',s));
      FLibList.Add(s);
    end;
    tmpLst.Free;  
    Free;
  end;
end;

procedure TSQLiteConfig.SaveToFile(AFileName: string);
var
  i: Integer;
begin
  with TIniFile.Create(AFileName) do
  begin
    WriteInteger(SECTION_INI, 'Lanuage', FLanguage);
    WriteInteger(SECTION_INI, 'Encoding', FEncodeFmt);
    WriteBool(SECTION_INI, 'BatchSQL', FBatchSQL);
    WriteBool(SECTION_INI, 'ReadOnly', FDataReadonly);
    WriteBool(SECTION_INI, 'Wordwrap', FWordWrap);
    WriteBool(SECTION_INI, 'AllowModify', FNoModify);

    WriteInteger(SECTION_INI, 'FontSize', FFontSize);
    WriteString(SECTION_INI, 'FontName', FFontName);
    WriteBool(SECTION_INI, 'ViewStatusBar', FViewStatusbar);
    WriteBool(SECTION_INI, 'ViewObjTree', FViewObjTree);
    WriteBool(SECTION_INI, 'ViewToolBar', FViewToolbar);

    WriteInteger(SECTION_INI, 'HKKey', FHKKey);
    WriteInteger(SECTION_INI, 'HKType', FHKDataType);
    WriteInteger(SECTION_INI, 'HKFunc', FHKFunc);

    WriteBool(SECTION_INI, 'UTF8FileName', FUTF8FileName);
    WriteString(SECTION_INI, 'CurrentLib', FCurrentLib);

    EraseSection(SECTION_LIB);
    //  write library list
    for i := 0 to FLibList.Count - 1 do
      WriteString(SECTION_LIB, Format('Lib%d',[i+1]), FLibList[i]);
      
    Free;
  end;
end;

procedure TSQLiteConfig.SetLibrarys(Index: integer; const Value: string);
begin
  FLibList[Index] := Value;
end;

end.
